﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.WindowsBase;
using Billing.DataObjects;

namespace Billing.Win
{
    public partial class AccountNumberIndexPrefixForm : BaseList
    {
        BillingDataContext mBillingDataContext = new BillingDataContext();

        public AccountNumberIndexPrefixForm()
        {
            InitializeComponent();
        }

        protected override void onDataLoad()
        {
            base.onDataLoad();
            this.bsElement.DataSource = mBillingDataContext.AccountNumberIndexPrefixes;
            this.ElementRowTemplate = ultraGridRowEditTemplate1;
        }
        protected override void onDelete()
        {
            try
            {
                base.onDelete();
                bsElement.RemoveCurrent();
                //using (StandardBank.DataAccess.FormDB objDB = new StandardBank.DataAccess.FormDB())
                //{
                //    objDB.delete(this.getCurrentElementRowID);
                //}
                this.onDataLoad();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        protected override void onSave()
        {
            base.onSave();
            mBillingDataContext.SubmitChanges();
        }
        protected override void onClose()
        {
            if (AllowSave && (mBillingDataContext.GetChangeSet().Deletes.Count > 0
                || mBillingDataContext.GetChangeSet().Updates.Count > 0
                || mBillingDataContext.GetChangeSet().Inserts.Count > 0))
            {
                if (MessageBox.Show("შევინახო შეცვლილი მონაცემები?", "", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    mBillingDataContext.SubmitChanges();
            }
            base.onClose();
        }

        protected override void onElementInitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            base.onElementInitializeLayout(sender, e);
            grd.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.RowSelect;
            if (e.Layout.Bands[0].Columns.Count > 0)
            {
                e.Layout.Bands[0].Columns["ID"].Hidden = true;
                e.Layout.Bands[0].Columns["CreatedDate"].Hidden = true;
                e.Layout.Bands[0].Columns["ModifiedDate"].Hidden = true;
                e.Layout.Bands[0].Columns["RowVersion"].Hidden = true;
                e.Layout.Bands[0].Columns["AccountNumberIndexID"].Hidden = true;
                e.Layout.Bands[0].Columns["AccountNumberPrefixID"].Hidden = true;

                e.Layout.Bands[0].Columns["AccountNumberIndex"].Header.Caption = "ინდექსი";
                e.Layout.Bands[0].Columns["AccountNumberIndex"].Width = 200;
                e.Layout.Bands[0].Columns["AccountNumberPrefix"].Header.Caption = "პრეფიქსი"; ;
                e.Layout.Bands[0].Columns["AccountNumberPrefix"].Width = 200;
            }
        }

        private void btnTemplateOk_Click(object sender, EventArgs e)
        {
            // This code was automatically generated by the RowEditTemplate Wizard
            // 
            // Close the template and save any pending changes.
            this.ultraGridRowEditTemplate1.Close(true);

        }

        private void btnTemplateCancel_Click(object sender, EventArgs e)
        {
            // This code was automatically generated by the RowEditTemplate Wizard
            // 
            // Close the template and discard any pending changes.
            this.ultraGridRowEditTemplate1.Close(false);

        }

        private void ultraButton2_Click(object sender, EventArgs e)
        {
            AccountNumberIndexForm form = new AccountNumberIndexForm(false);
            form.AllowAdd = false;
            form.AllowDelete = false;
            form.AllowEdit = false;
            form.AllowSave = false;
            form.AllowSelect = true;
            if (form.ShowDialog() == DialogResult.OK)
            {
                AccountNumberIndex accountNumberIndex = ((AccountNumberIndex)form.SelectedObjects[0].CurrentObject);
                (this.ultraGridRowEditTemplate1.Row.ListObject as AccountNumberIndexPrefix).AccountNumberIndex =
                    (from n in mBillingDataContext.AccountNumberIndexes
                     where n.ID == accountNumberIndex.ID
                     select n).First();
            }
        }

        private void ultraButton1_Click(object sender, EventArgs e)
        {
            AccountNumberPrefixForm form = new AccountNumberPrefixForm(false);
            form.AllowAdd = false;
            form.AllowDelete = false;
            form.AllowEdit = false;
            form.AllowSave = false;
            form.AllowSelect = true;
            if (form.ShowDialog() == DialogResult.OK)
            {
                AccountNumberPrefix accountNumberPrefix = ((AccountNumberPrefix)form.SelectedObjects[0].CurrentObject);
                (this.ultraGridRowEditTemplate1.Row.ListObject as AccountNumberIndexPrefix).AccountNumberPrefix = 
                    (from n in mBillingDataContext.AccountNumberPrefixes
                            where n.ID == accountNumberPrefix.ID
                            select n).First();
            }
        }
    }
}
